package com.bos.controller.json;

import com.bos.dao.AnggaranDAO;
import com.bos.dao.JumlahSiswaDAO;
import com.bos.dao.KecamatanDAO;
import com.bos.dao.PeriodeDAO;
import com.bos.domain.Anggaran;
import com.bos.domain.JumlahSiswa;
import com.bos.domain.Periode;
import com.bos.editor.JenjangSekolahEditor;
import com.bos.editor.PeriodeEditor;
import com.bos.model.Bos02Request;
import com.bos.model.DayaSerapRequest;
import com.bos.report.factory.Bos02Factory;
import com.bos.report.factory.DanaBosKecamatanFactory;
import com.bos.report.factory.DayaSerapFactory;
import com.bos.util.CalendarUtil;
import com.bos.util.enums.JenjangSekolah;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ReportJsonController {

    private JumlahSiswaDAO jumlahSiswaDAO;
    private AnggaranDAO anggaranDAO;
    private PeriodeDAO periodeDAO;
    private KecamatanDAO kecamatanDAO;

    private Bos02Factory bos02Factory;
    private DayaSerapFactory dayaSerapFactory;
    private DanaBosKecamatanFactory danaBosKecamatanFactory;

    /**
     * ***************************** SETTER GETTER **********************************************************************
     */
    @Autowired
    public void setJumlahSiswaDAO(JumlahSiswaDAO jumlahSiswaDAO) {
        this.jumlahSiswaDAO = jumlahSiswaDAO;
    }

    @Autowired
    public void setAnggaranDAO(AnggaranDAO anggaranDAO) {
        this.anggaranDAO = anggaranDAO;
    }

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setKecamatanDAO(KecamatanDAO kecamatanDAO) {
        this.kecamatanDAO = kecamatanDAO;
    }

    @Autowired
    public void setBos02Factory(Bos02Factory bos02Factory) {
        this.bos02Factory = bos02Factory;
    }

    @Autowired
    public void setDayaSerapFactory(DayaSerapFactory dayaSerapFactory) {
        this.dayaSerapFactory = dayaSerapFactory;
    }

    @Autowired
    public void setDanaBosKecamatanFactory(DanaBosKecamatanFactory danaBosKecamatanFactory) {
        this.danaBosKecamatanFactory = danaBosKecamatanFactory;
    }

    /**
     * ************* JSON **********************************************************************************************
     */

    @RequestMapping(value = "/report/bos02.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map downloadBos02(@RequestBody Bos02Request bos02Request) {
        if (bos02Request == null || bos02Request.getJenjangSekolah() == null || bos02Request.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Ambil sumber data source
        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(bos02Request.getJenjangSekolah(), bos02Request.getPeriode());
        List<JumlahSiswa> jumlahSiswas = jumlahSiswaDAO.getByJenjangSekolahPeriode(bos02Request.getJenjangSekolah(), bos02Request.getPeriode());

        //Isikan datasource
        bos02Factory.fillDataSource(bos02Request.getJenjangSekolah(), anggarans, jumlahSiswas);

        //isikan parameter
        bos02Factory.putResponseParameter(Bos02Factory.KEY_KAB_KOTA, "Kabupaten");
        bos02Factory.putResponseParameter(Bos02Factory.KEY_NAMA_KAB, "Aceh Besar");
        bos02Factory.putResponseParameter(Bos02Factory.KEY_TANGGAL, CalendarUtil.getCalendarStringFormat());
        bos02Factory.putResponseParameter(Bos02Factory.KEY_PERIODE, bos02Request.getPeriode().getTriwulan().getBulanAwal() + "-" + bos02Request.getPeriode().getTriwulan().getBulanAkhir() + " " + bos02Request.getPeriode().getTahun().getTahun());

        return bos02Factory.getResponseParameter();
    }

    @RequestMapping(value = "/report/dayaSerap.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map downloadDayaSerap(@RequestBody DayaSerapRequest dayaSerapRequest) {
        if (dayaSerapRequest == null || dayaSerapRequest.getJenjangSekolah() == null || dayaSerapRequest.getPeriode() == null)
            throw new IllegalArgumentException("A Request is required");

        //Ambil sumber data source
        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(dayaSerapRequest.getJenjangSekolah(), dayaSerapRequest.getPeriode());
        List<JumlahSiswa> jumlahSiswas = jumlahSiswaDAO.getByJenjangSekolahPeriode(dayaSerapRequest.getJenjangSekolah(), dayaSerapRequest.getPeriode());

        //Isikan datasource
        dayaSerapFactory.fillDataSource(dayaSerapRequest.getJenjangSekolah(), anggarans, jumlahSiswas);

        //isikan parameter
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_KAB_KOTA, "Kabupaten");
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_NAMA_KAB, "Aceh Besar");
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_TANGGAL, CalendarUtil.getCalendarStringFormat());
        dayaSerapFactory.putResponseParameter(DayaSerapFactory.KEY_PERIODE, dayaSerapRequest.getPeriode().getTriwulan().getBulanAwal() + "-" + dayaSerapRequest.getPeriode().getTriwulan().getBulanAkhir() + " " + dayaSerapRequest.getPeriode().getTahun().getTahun());

        return dayaSerapFactory.getResponseParameter();
    }

    @RequestMapping(value = "/report/chart/danaBosKecamatan/jenjangSekolah/{jenjangSekolah}/periode/{periode}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map createDanaBosKecamatans(@PathVariable("jenjangSekolah")JenjangSekolah jenjangSekolah, @PathVariable("periode") Periode periode) {

        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(jenjangSekolah, periode);

        danaBosKecamatanFactory.fillDataSource(jenjangSekolah, periode, 2L, anggarans);

        return danaBosKecamatanFactory.getResponseParameter();
    }

    /**
     * ************** OTHER METHOD *************************************************************************************
     */

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(JenjangSekolah.class, new JenjangSekolahEditor());
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }
}
